package cn.aran.user.service.impl;

import cn.aran.basic.constants.BaseConstants;
import cn.aran.basic.exception.BusinessException;
import cn.aran.basic.utils.MD5Utils;
import cn.aran.basic.utils.StrUtils;
import cn.aran.basic.vo.EmailRegisterVo;
import cn.aran.basic.vo.RegisterVo;
import cn.aran.user.domain.Logininfo;
import cn.aran.user.domain.User;
import cn.aran.user.mapper.LogininfoMapper;
import cn.aran.user.mapper.UserMapper;
import cn.aran.user.service.IUserService;
import cn.aran.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aran
 * @since 2022-07-04
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private UserMapper userMapper;

    /**
    * @Title: phoneRegister
    * @Description: 手机登录
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/6 18:27
    * @Parameters: [phoneRegisterVo]
    * @Return void
    */
    @Override
    public void phoneRegister(RegisterVo phoneRegisterVo) {
        String phone = phoneRegisterVo.getPhone();
        String smsCodeValue = phoneRegisterVo.getSmsCodeValue();
        String password = phoneRegisterVo.getPassword();
        String passwordRepeat = phoneRegisterVo.getPasswordRepeat();
        // 判断参数是否为空
        if (StringUtils.isEmpty(phone)
            || StringUtils.isEmpty(smsCodeValue)
            || StringUtils.isEmpty(password)
            || StringUtils.isEmpty(passwordRepeat)) {
            throw new BusinessException("必要参数不能为空!");
        }
        // 判断两次密码是否一致
        if (!password.equals(passwordRepeat)){
            throw new BusinessException("密码不一致!");
        }
        // 判断验证码是否存在
        String smsCodeKey = BaseConstants.VerifyCodeConstant.PHONE_REGISTER_PREFIX + phone;
        Object smsCodeRedis = redisTemplate.opsForValue().get(smsCodeKey);
        if (smsCodeRedis == null){
            throw new BusinessException("验证码已过期,请重新获取!");
        }
        // 判断验证码是否一致
        String[] smsCodeRedises = smsCodeRedis.toString().split(":");
        if (!smsCodeRedises[0].equalsIgnoreCase(smsCodeValue)){
            throw new BusinessException("验证码错误,请重新输入!");
        }
        // 判断用户是否存在
        User user = userMapper.loadByPhone(phone);
        if (user != null){
            throw new BusinessException("用户已注册，请直接登录，如果忘记密码，请点击忘记密码！");
        }
        // 保存到user表中
        user = RegisterVo2User(phoneRegisterVo);
        // 将user表中的数据拷贝到logininfo中
        Logininfo logininfo = user2Logininfo(user);
        // 保存到logininfo
        logininfoMapper.save(logininfo);
        // 设置user表中logininfo_id
        user.setLogininfoId(logininfo.getId());
        // 保存到user中
        userMapper.save(user);
    }

    /**
    * @Title: emailRegister
    * @Description: 邮箱注册
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/9 17:59
    * @Parameters: [emailRegisterVo]
    * @Return void
    */
    @Override
    public void emailRegister(EmailRegisterVo emailRegisterVo) {
        String email = emailRegisterVo.getEmail();
        String emailCodeValue = emailRegisterVo.getEmailCodeValue();
        String password = emailRegisterVo.getPassword();
        String passwordRepeat = emailRegisterVo.getPasswordRepeat();
        // 判断参数是否为空
        if (StringUtils.isEmpty(email)
            || StringUtils.isEmpty(emailCodeValue)
            || StringUtils.isEmpty(password)
            || StringUtils.isEmpty(passwordRepeat)) {
            throw new BusinessException("必要参数不能为空!");
        }
        // 判断两次密码是否一致
        if (!password.equals(passwordRepeat)){
            throw new BusinessException("密码不一致!");
        }
        // 判断验证码是否存在
        String emailCodeKey = BaseConstants.VerifyCodeConstant.EMAIL_REGISTER_PREFIX + email;
        Object emailCodeRedis = redisTemplate.opsForValue().get(emailCodeKey);
        if (emailCodeRedis == null){
            throw new BusinessException("验证码已过期,请重新获取!");
        }
        // 判断验证码是否一致
        String[] smsCodeRedises = emailCodeRedis.toString().split(":");
        if (!smsCodeRedises[0].equalsIgnoreCase(emailCodeValue)){
            throw new BusinessException("验证码错误,请重新输入!");
        }
        // 判断用户是否已注册
        User user = userMapper.loadByEmail(email);
        if (user != null){
            throw new BusinessException("用户已注册，请直接登录，如果忘记密码，请点击忘记密码！");
        }
        // 保存到user表中
        user = EmailRegisterVo2User(emailRegisterVo);
        // 将user表中的数据拷贝到logininfo中
        Logininfo logininfo = user2Logininfo(user);
        // 保存到logininfo
        logininfoMapper.save(logininfo);
        // 设置user表中logininfo_id
        user.setLogininfoId(logininfo.getId());
        // 保存到user中
        userMapper.save(user);
    }

    /**
    * @Title: loadByLogininfoId
    * @Description: 根据登录id查询user用户
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/19 19:09
    * @Parameters: [id]
    * @Return cn.aran.user.domain.User
    */
    @Override
    public User loadByLogininfoId(Long id) {
        return userMapper.loadByLogininfoId(id);
    }

    /**
    * @Title: EmailRegisterVo2User
    * @Description: 将注册信息保存到user表中
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/9 18:16
    * @Parameters: [emailRegisterVo]
    * @Return cn.aran.user.domain.User
    */
    private User EmailRegisterVo2User(EmailRegisterVo emailRegisterVo) {
        User user = new User();
        user.setUsername(emailRegisterVo.getEmail());
        user.setEmail(emailRegisterVo.getEmail());
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + emailRegisterVo.getPassword());
        user.setSalt(salt);
        user.setPassword(saltPassword);
        return user;
    }

    /**
    * @Title: RegisterVo2User
    * @Description: 将注册信息保存到user表中
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/6 19:13
    * @Parameters: [phoneRegisterVo]
    * @Return cn.aran.user.domain.User
    */
    private User RegisterVo2User(RegisterVo phoneRegisterVo) {
        User user = new User();
        user.setUsername(phoneRegisterVo.getPhone());
        user.setPhone(phoneRegisterVo.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + phoneRegisterVo.getPassword());
        user.setSalt(salt);
        user.setPassword(saltPassword);
        return user;


    }

    /**
    * @Title: user2Logininfo
    * @Description: 将user表中的数据拷贝到logininfo中
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/6 19:13
    * @Parameters: [user]
    * @Return cn.aran.user.domain.Logininfo
    */
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        return logininfo;
    }

}
